package com.um.jdy.platform.station.manager;

import cn.hutool.core.bean.BeanUtil;
import com.um.jdy.common.device.entity.po.Fee;
import com.um.jdy.common.device.entity.po.FeePbPrice;
import com.um.jdy.common.device.entity.vo.FeePbPriceVO;
import com.um.jdy.common.device.service.FeePbPriceService;
import com.um.jdy.common.device.service.FeeService;
import com.um.jdy.platform.station.entity.dto.FeePbDTO;
import com.um.jdy.platform.station.entity.vo.FeePbVO;
import com.um.jdy.rpc.enums.DeviceTypeEnum;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.utils.UMBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class FeePbManager extends BaseManager<FeePbPriceService> {

    @Autowired
    private FeeService feeService;

    public FeePbVO get(String id) {
        Fee fee = feeService.getEntity("device_fee_id", id);

        FeePbPrice feePbPrice = service.getEntity("device_fee_id", id);

        feePbPrice.setDuration(feePbPrice.getDuration() / 60);
        feePbPrice.setFreeDuration(feePbPrice.getFreeDuration() / 60);

        FeePbVO feePricePowerVO = new FeePbVO();
        feePricePowerVO.setName(fee.getName());
        feePricePowerVO.setPrices(BeanUtil.toBean(feePbPrice, FeePbPriceVO.class));
        return feePricePowerVO;
    }

    @Transactional
    public FeePbVO add(FeePbDTO feePbDTO) {

        if(feePbDTO.getPrices().getPrice() > feePbDTO.getPrices().getDaysAmount()){
            throw new ServiceException("收费单价不能大于24小时金额");
        }

        if(feePbDTO.getPrices().getDaysAmount() > feePbDTO.getPrices().getMaxAmount()){
            throw new ServiceException("24小时金额不能大于封顶金额");
        }

        if(feePbDTO.getPrices().getMaxAmount() > feePbDTO.getPrices().getDeposit()){
            throw new ServiceException("封顶金额不能大于押金金额");
        }

        Fee fee = UMBeanUtils.copyBean(feePbDTO,Fee.class);
        fee.setCreateTime(new Date());
        fee.setDeviceType(DeviceTypeEnum.PB.getKey());
        feeService.addEntity(fee);

        FeePbPrice feePbPrice = BeanUtil.toBean(feePbDTO.getPrices(), FeePbPrice.class);
        feePbPrice.setDeviceFeeId(fee.getDeviceFeeId());
        feePbPrice.setDuration(feePbDTO.getPrices().getDuration() * 60);
        feePbPrice.setFreeDuration(feePbDTO.getPrices().getFreeDuration() * 60);
        service.add(feePbPrice);

        return BeanUtil.toBean(feePbDTO, FeePbVO.class);
    }

    @Transactional
    public FeePbVO update(String id, FeePbDTO feePbDTO) {

        if(feePbDTO.getPrices().getPrice() > feePbDTO.getPrices().getDaysAmount()){
            throw new ServiceException("收费单价不能大于24小时金额");
        }

        if(feePbDTO.getPrices().getDaysAmount() > feePbDTO.getPrices().getMaxAmount()){
            throw new ServiceException("24小时金额不能大于封顶金额");
        }

        if(feePbDTO.getPrices().getMaxAmount() > feePbDTO.getPrices().getDeposit()){
            throw new ServiceException("封顶金额不能大于押金金额");
        }


        Fee fee = UMBeanUtils.copyBean(feePbDTO,Fee.class);
        fee.setUpdateTime(new Date());
        fee.setDeviceFeeId(id);
        feeService.updateEntity(fee);

        //删除原价格列表
        service.delete("device_fee_id", id);

        FeePbPrice feePbPrice = BeanUtil.toBean(feePbDTO.getPrices(), FeePbPrice.class);
        feePbPrice.setDeviceFeeId(id);
        feePbPrice.setDuration(feePbDTO.getPrices().getDuration() * 60);
        feePbPrice.setFreeDuration(feePbDTO.getPrices().getFreeDuration() * 60);
        service.add(feePbPrice);

        return BeanUtil.toBean(feePbDTO, FeePbVO.class);
    }
}
